#ifndef MYMUSIC_WLAUDIO_H
#define MYMUSIC_WLAUDIO_H

#include "../bean/WlQueue.h"
#include "../bean/WlPlaystatus.h"
#include "../bean/WlCallJava.h"

extern "C"
{
#include "../thirdpart/libavutil/time.h"
#include "../thirdpart/libavcodec/avcodec.h"
#include "../thirdpart/libswresample/swresample.h"
#include <SLES/OpenSLES_Android.h>
};

class WlAudio {

public:
    int streamIndex = -1;
    int ret = 0;
    uint8_t *buffer = NULL;
    int data_size = 0;
    /**当前音频的采样率*/
    int sample_rate = 0;
    /**音频总长*/
    int duration = 0;
    /**当前时长*/
    double clock;
    /**当前frame时间*/
    double now_time;
    /**上一次调用时间*/
    double last_tiem;

    pthread_t thread_play;
    pthread_mutex_t codecMutex;

    AVCodecContext *avCodecContext = NULL;
    AVCodecParameters *codecpar = NULL;
    AVPacket *avPacket = NULL;
    AVFrame *avFrame = NULL;
    AVRational time_base;

    WlQueue *queue = NULL;
    WlPlaystatus *playstatus = NULL;
    WlCallJava *callJava = NULL;

    SLObjectItf engineObject = NULL;
    SLObjectItf outputMixObject = NULL;
    SLObjectItf pcmPlayerObject = NULL;
    SLEngineItf engineEngine = NULL;
    SLEnvironmentalReverbItf outputMixEnvironmentalReverb = NULL;
    SLEnvironmentalReverbSettings reverbSettings = SL_I3DL2_ENVIRONMENT_PRESET_STONECORRIDOR;
    SLPlayItf pcmPlayerPlay = NULL;
    SLAndroidSimpleBufferQueueItf pcmBufferQueue = NULL;

public:
    WlAudio( WlPlaystatus *playstatus, int sample_rate, WlCallJava *callJava);
    ~WlAudio();

    /**音频重采样*/
    int resampleAudio();
    void initOpenSLES();
    /** 解码获得的采样率 和opensl需要的采样率进行转换*/
    SLuint32 getCurrentSampleRateForOpensles(int sample_rate);
    void play();
    void pause();
    void resume();
    void stop();
    void release();
};


#endif //MYMUSIC_WLAUDIO_H
